﻿@using System.Globalization
@using LERP.Domain.Enums
@using Sharp.Framework
@using Sharp.Framework.Extensions
@model LERP.Web.Controllers.ViewModels.OrderViewModels.CreateOrderViveModel
@{
    ViewBag.Title = "新建订单";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var orderStatus = OrderStatus.Close.ToDictionary().Where(
                    m => m.Value == (int)OrderStatus.Draft||m.Value == (int)OrderStatus.ToBeShipped).Select(
                        m => new SelectListItem { Text = m.Key, Value = m.Value.ToString(CultureInfo.InvariantCulture) }).
                    ToList();
}
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset> 
        <legend>基本信息 </legend>
        <ul class="list_form">
            <li>
                @Html.LabelFor(model => model.Customer)   
                @Html.TextBoxFor(model => model.Customer, new {@class = "text-long",@readonly ="readonly"})    <a href="@Url.Action("Select", "Customer")" id="customer_select" style="">选择客户</a>
                @Html.ValidationMessageFor(model => model.Customer)   
                @Html.HiddenFor(model => model.CustomerId) 
                @Html.HiddenFor(model => model.Status)
            </li>
               <li>
                @Html.LabelFor(model => model.Contract)   
                @Html.DropDownListFor(m => m.Contract, (ViewBag.Contracts as List<SelectListItem>).InsertEmpty(), new { @class = "text-long" })
                @Html.ValidationMessageFor(model => model.Contract)
                @Html.HiddenFor(model => model.Contract)
            </li>
            <li>
                @Html.LabelFor(model => model.OrderDate)   
                @Html.TextBoxFor(model => model.OrderDate, new { @class = "text-long dataselect" })
                @Html.ValidationMessageFor(model => model.OrderDate) 
            </li>
            <li>
                @Html.LabelFor(model => model.ExpectDelivery)   
                @Html.TextBoxFor(model => model.ExpectDelivery, new { @class = "text-long dataselect" })
                @Html.ValidationMessageFor(model => model.ExpectDelivery)
            </li>
            <li>
                @Html.LabelFor(model => model.CustomerOrderNo)   
                @Html.TextBoxFor(model => model.CustomerOrderNo, new {@class = "text-long"}) 
                @Html.ValidationMessageFor(model => model.CustomerOrderNo)
            </li>
        
            <li>
                @Html.LabelFor(model => model.Remark)   
                @Html.TextAreaFor(model => model.Remark, new {rows = "1", cols = "1"}) 
                @Html.ValidationMessageFor(model => model.Remark)
            </li>
            <li>
                <label>是否关联货品</label>  
                <input name="@Html.IdFor(m => m.IsGoods)" id="@Html.IdFor(m => m.IsGoods)" @((Model.IsGoods) ? "checked=\"checked\"" : "") value="true"  type="checkbox" /> 
                @Html.LabelFor(model => model.IsGoods, new {style = "text-align:left;"})
                @Html.ValidationMessageFor(model => model.Amount)
            </li> 
            <li id="li_Amount" style="display: @((!Model.IsGoods) ? "" : "none");">
                @Html.LabelFor(model => model.Amount)   
                @Html.TextBoxFor(model => model.Amount, new {@class = "text-long"})
            </li>
        </ul> 
    </fieldset>
    <fieldset id="fieldset_goods" style="display: @((Model.IsGoods) ? "" : "none");"> 
        <legend>货品明细</legend>
        <a href="@Url.Action("Select", "Goods")" id="goods_select" style="float: right; margin-bottom: 5px;">选择</a>
        <table class="table_list" id="goods_table" style="width: 99%; display: none;">
            <tr>
                <th style="width: 80px;">国际条码</th>
                <th>品名规格</th>
                <th style="width: 80px;">单价</th>
                <th style="width: 30px;">单位</th>
                <th style="width: 60px;">订单数量</th>
                <th style="width: 50px;">操作</th>
            </tr>             
        </table>
        <div style="float: right; margin-top: 25px; width: 300px; text-align: right;">
            <div style="margin-bottom: 5px;">订单金额: <label id="order_sum">0</label> 元 
                @Html.HiddenFor(model => model.OrderItems)
            </div> 
        </div>
    </fieldset>
    <input type="submit"  class="button-submit" value="保存" />
    <br/>
    <br/>
    <br/>
}

@section scripts
{
    <script type="text/javascript">
        var goodsitemtemplate = "<tr id=\"goods_{0}\"><td>{1}</td><td title=\"{2}\">{3}</td><td><input class=\"text-small\" goodsid=\"{0}\"  name=\"price\" type=\"text\" value=\"{4}\"  olevalue=\"{4}\" /></td><td>{5}</td><td><input class=\"text-small\" goodsid=\"{0}\"  name=\"num\" type=\"text\" value=\"{6}\" /></td><td class=\"action\"><a  goodsid=\"{0}\"   href=\"javascript:void(0);\">取消</a></td></tr>";
        var goodsList = new Array();
        
        $(function () {
            //加载返回的订单项
            var $orderItems = $("#@Html.IdFor(m=>m.OrderItems)");
            if ($orderItems.val() != '') {
                goodsList = JSON.parse($orderItems.val());
                for (var i = 0; i < goodsList.length; i++) {
                    var item = goodsList[i];
                    $("#goods_table").append(String.format(goodsitemtemplate, item.id, item.gencod, item.name, item.shortname, item.price, item.sellunit, item.num));
                }
                $("#goods_table").show(); 
                statisticsOrders();
            }


            //选择客户
            $("#customer_select").click(function () {
                var iTop = (window.screen.availHeight - 30 - 600) / 2; //获得窗口的垂直位置;
                var iLeft = (window.screen.availWidth - 10 - 750) / 2; //获得窗口的水平位置;
                var url = this.href;
                window.open(url, "选择客户", "height=600, width=750, toolbar=no, menubar=no, resizable=no, location=no, status=no,scrollbars=yes" + ',top=' + iTop + ',left=' + iLeft);
                return false;
            });

            //关联货品切换
            $("#@Html.IdFor(m=>m.IsGoods)").click(function () {
                if (this.checked) {
                    $("#fieldset_goods").show();
                    $("#li_Amount").hide();
                } else {
                    $("#li_Amount").show();
                    $("#fieldset_goods").hide(); 
                }
            });


            //选择货品
            $("#goods_select").click(function() {
                var iTop = (window.screen.availHeight - 30 - 600) / 2; //获得窗口的垂直位置;
                var iLeft = (window.screen.availWidth - 10 - 750) / 2; //获得窗口的水平位置;
                var url = this.href;
                var customerId = $("#@Html.IdFor(x=>x.CustomerId)").val();
                if ( customerId!= "") {
                    url += "/" + customerId;
                }
                window.open(url, "选择发货货品", "height=600, width=750, toolbar=no, menubar=no, resizable=no, location=no, status=no,scrollbars=yes" + ',top=' + iTop + ',left=' + iLeft);

                return false;
            });
            //修改货品数量
            $("#goods_table input[name='num']").live("change", function () {
                if (!new RegExp("^\\d+$").test(this.value)) {
                    this.value = this.value.replace(/\D/g, '');
                    alert("只能输入正整数");
                    return false;
                }
                var goodsid = $(this).attr("goodsid");
                for (var i = 0; i < goodsList.length; i++) {
                    var item = goodsList[i];
                    if (item.id == goodsid) {
                        item.num = $(this).val();
                        break;
                    }
                }
                statisticsOrders();
            });
            

            //修改货品价格
            $("#goods_table input[name='price']").live("change", function () {
                if (!new RegExp("^([1-9]\\d*(\\.\\d{1,2})?|0\\.[1-9]\\d?|0\\.0[1-9]|0|0.0)$").test(this.value)) {
                    this.value = $(this).attr("olevalue");
                    alert("只能输入的数字");
                    return false;
                }
                $(this).attr("olevalue", this.value);
                var goodsid = $(this).attr("goodsid");
                for (var i = 0; i < goodsList.length; i++) {
                    var item = goodsList[i];
                    if (item.id == goodsid) {
                        item.price = $(this).val();
                        break;
                    }
                }
                statisticsOrders();
            });
            //移除货品
            $("#goods_table .action a").live("click", function () {
                var goodsid = $(this).attr("goodsid");
                for (var i = 0; i < goodsList.length; i++) {
                    var item = goodsList[i];
                    if (item.id == goodsid) { 
                        goodsList.remove(goodsList.getIndexByValue(item));  
                        break;
                    }
                }
                $("#goods_" + goodsid).remove();
                if (goodsList.length == 0) {
                    $("#goods_table").hide();
                }
                statisticsOrders();
                return false;
            });

        });
        

        //选择/取消货品完成事件
        var goodsCommandCompleted = function (command, itemString) {
            var item = JSON.parse(itemString);
            if (command == "add") {
                goodsList.push(item);
                $("#goods_table").append(String.format(goodsitemtemplate, item.id, item.gencod, item.name, item.shortname, item.price, item.sellunit, item.num));
                if(goodsList.length>0) {
                    $("#goods_table").show();
                }
            } else if (command == "del") {
                for (var i = 0; i < goodsList.length; i++) {
                    if(goodsList[i].id==item.id) {
                        goodsList.remove(goodsList.getIndexByValue(item));
                        break;
                    }
                }
                $("#goods_" + item.id).remove(); 
                if (goodsList.length == 0) {
                    $("#goods_table").hide();
                }
            }
            statisticsOrders();
        };
        var statisticsOrders = function() {
            var sum = 0;
            for (var i = 0; i < goodsList.length; i++) {
                var item = goodsList[i];
                sum += new Number(item.price) * new Number(item.num);
            }
            $("#order_sum").html(sum);
            $("#@Html.IdFor(m=>m.Amount)").val(sum);
            $("#@Html.IdFor(m=>m.OrderItems)").val(JSON.stringify(goodsList));
        };

</script>

}
